// ==UserScript==
// @name         Kilicen-apw
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  自动填入缺陷修改格式
// @author       tangxiaobin
// @match        http://jira.iklicen.com/*
// @grant        none
// @updateURL    https://gitee.com/txbin/klicen-apw/blob/master/KlicenAPW.js

// ==/UserScript==
(function () {
    "use strict";
    console.log("Tang: strict", "加载脚本");
    init();

    function init() {
        window.addEventListener("click", run);
    }

    window.onload = function () {
        setTimeout(() => {
            const list = document.getElementsByClassName("js-detailview");
            for (let i = 0, len = list.length; i < len; i++) {
                const element = list[i];
                element.onclick = copyAPW;
            }
        }, 3000);
    };

    function run(e) {
        const className = e.target.className;
        if (className.includes("textarea long-field")) {
            setValue(e);
        }
    }

    function setValue(e) {
        const preEleInnerHtml = e.target.previousElementSibling.innerHTML;
        if (!preEleInnerHtml.includes("RootCause&amp;解决方案")) return;

        if (!e.target.innerHTML) {
            e.target.innerHTML = `[RootCause]：
                
[解决方案]：
    `;
        }
    }

    function copyAPW() {
        const apw = this.getAttribute("data-issue-key");
        console.log("Tang: copyAPW -> apw", apw);
        if (/^apw-\d+$/i.test(apw)) {
            copyToClipboard(`[#${apw}]`);
        } else {
            showMessage("未找到复合条件缺陷编号" + apw, "error");
        }
    }

    function copyToClipboard(str) {
        // 创建元素用于复制
        var aux = document.createElement("input");
        // 设置元素内容
        aux.setAttribute("value", str);

        // 将元素插入页面进行调用
        document.body.appendChild(aux);

        // 复制内容
        aux.select();

        // 将内容复制到剪贴板
        document.execCommand("copy");

        // 删除创建元素
        document.body.removeChild(aux);

        //提示
        showMessage(aux.value, "success");
    }

    function showMessage(message, type, time = 3000) {
        let str = "";
        switch (type) {
            case "success":
                str =
                    '<div class="success-message" style="width: 300px;height: 40px;text-align: center;background-color:#F0F9EB;;color: #67C23A;position: fixed;left: 43%;top: 10%;line-height: 40px;border-radius: 5px;z-index: 9999">\n' +
                    '    <span class="mes-text">' +
                    message +
                    "</span></div>";
                break;
            case "error":
                str =
                    '<div class="error-message" style="width: 300px;height: 40px;text-align: center;background-color: #f5f0e5;color: rgba(238,99,99,0.8);position: fixed;left: 43%;top: 10%;line-height: 40px;border-radius: 5px;;z-index: 9999">\n' +
                    '    <span class="mes-text">' +
                    message +
                    "</span></div>";
        }
        jQuery("body").append(str);
        setTimeout(function () {
            jQuery("." + type + "-message").remove();
        }, time);
    }
})();
